package com.yljk.homedoctor.view.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.tabs.TabLayout;
import com.yljk.homedoctor.R;


public class CustomTabLayout extends TabLayout implements TabLayout.OnTabSelectedListener {
    private Context context;
    public CustomTabLayout(@NonNull Context context) {
        super(context);
        init(context);
    }

    public CustomTabLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomTabLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.context = context;
        addOnTabSelectedListener((OnTabSelectedListener) this);
    }


    @Override
    public void selectTab(@Nullable Tab tab) {
        super.selectTab(tab);
//        TextView textView = new TextView(getContext());
//        textView.setText(tab.getText());
//        textView.setGravity(Gravity.CENTER);
//        float selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 16, getResources().getDisplayMetrics());
//        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedSize);
//        textView.setTextColor(Color.BLACK);
//        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
////                tabLayout.setTabIndicatorFullWidth(false);
//        tab.setCustomView(textView);
    }


    @Override
    public void onTabSelected(Tab tab) {

//        mViewPager.setCurrentItem(tab.getPosition());
        View view = tab.getCustomView();
        if (null == view) {
            tab.setCustomView(R.layout.tab_layout_text);
        }
        TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
        textView.setTextAppearance(context, R.style.TabLayoutTextSize_select);

    }

    @Override
    public void onTabUnselected(Tab tab) {
//        mViewPager.setCurrentItem(tab.getPosition());
        View view = tab.getCustomView();
        if (null == view) {
            tab.setCustomView(R.layout.tab_layout_text);
        }
        TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
        textView.setTextAppearance(context, R.style.TabLayoutTextSize_unselect);

    }

    @Override
    public void onTabReselected(Tab tab) {

    }
}
